// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DashboardViewModel.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the DashboardViewModel type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Windows.Client.ViewModel
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Diagnostics.Contracts;
    using System.Linq;
    using Business.Interfaces;
    using Common;
    using Hisaab.Common;

    using Microsoft.Practices.ServiceLocation;

    using Model;

    /// <summary>
    /// Defines the <seealso cref="Hisaab.Windows.Client.ViewModel.DashboardViewModel"/> type.
    /// </summary>
    public class DashboardViewModel : ViewModelBase
    {
        /// <summary>
        /// The refresh command
        /// </summary>
        public static readonly ActiveAwareCommand RefreshCommand = new ActiveAwareCommand();

        /// <summary>
        /// Session manager
        /// </summary>
        private readonly ISession _session;

        /// <summary>
        /// Account manager
        /// </summary>
        private readonly IAccountManager _accountManager;

        /// <summary>
        /// Backing field for account total
        /// </summary>
        private ObservableCollection<KeyValue<Account, decimal>> _accountTotals;

        /// <summary>
        /// Backing field for monthly totals
        /// </summary>
        private ObservableCollection<AccountHistoryData> _monthlyTotals;

        /// <summary>
        /// Initializes a new instance of the <see cref="DashboardViewModel"/> class.
        /// </summary>
        public DashboardViewModel()
            : this(
            ServiceLocator.Current.GetInstance<IAccountManager>(),
            ServiceLocator.Current.GetInstance<ISession>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DashboardViewModel"/> class.
        /// </summary>
        /// <param name="accountManager">The account manager.</param>
        /// <param name="session">The Session</param>
        public DashboardViewModel(IAccountManager accountManager, ISession session)
        {
            Contract.Requires<ArgumentNullException>(accountManager != null);
            Contract.Requires<ArgumentNullException>(session != null);
            _accountManager = accountManager;
            _session = session;

            Refresh(null);
        }

        /// <summary>
        /// Gets or sets the account totals.
        /// </summary>
        /// <value>The account totals.</value>
        public ObservableCollection<KeyValue<Account, decimal>> AccountTotals
        {
            get
            {
                return _accountTotals;
            }

            set
            {
                _accountTotals = value;
                OnPropertyChanged("AccountTotals");
            }
        }

        /// <summary>
        /// Gets or sets the monthly totals.
        /// </summary>
        /// <value>
        /// The monthly totals.
        /// </value>
        public ObservableCollection<AccountHistoryData> MonthlyTotals
        {
            get
            {
                return _monthlyTotals;
            }

            set
            {
                _monthlyTotals = value;
                OnPropertyChanged("MonthlyTotals");
            }
        }

        /// <summary>
        /// Refreshes this data.
        /// </summary>
        /// <param name="param">The param.</param>
        public void Refresh(object param)
        {
            var totals = _accountManager.GetAccountTotals(_session.LoggedUser).Select(x => new KeyValue<Account, decimal> { Key = x.Key.Map<Business.Entities.Account, Account>(), Value = x.Value });
            AccountTotals = new ObservableCollection<KeyValue<Account, decimal>>(totals);

            var list = new List<AccountHistoryData>();
            var accountHistory = _accountManager.GetAllAccountBalance(_session.LoggedUser);
            foreach (var value in accountHistory)
            {
                var accountHistoryData = new AccountHistoryData { Title = value.Key.Name };
                foreach (var data in value.Value)
                {
                    accountHistoryData.Add(data.Map<Business.Entities.AccountMonthlyTotal, AccountMonthlyTotal>());
                }

                list.Add(accountHistoryData);
            }

            MonthlyTotals = new ObservableCollection<AccountHistoryData>(list);
        }
    }
}